﻿using MusicPlayerPlus.Interface.Base;
using MusicPlayerPlus.Interface.Model.Muisc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MusicPlayerPlus.PlugIn.DownloadMusic
{
    public class DownloadMusic : InterfaceBase,
        //Input
        MusicPlayerPlus.Interface.Base.Action.Input.IAutoExecuteAction,

        //Output
        MusicPlayerPlus.Interface.Base.Action.Output.IAddToMenuAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IGetPlayListAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IChangePlayListAction
    {



        #region Input

        /// <summary>
        /// 自动执行动作接口
        /// </summary>
        public void AutoExecute()
        {
            AddToMenu_Action("下载", () =>
            {
                LoadData();
                MainWindow mainWindow = new MainWindow();
                mainWindow.Show();
            });
        }

        #endregion

        #region Output

        /// <summary>
        /// 添加菜单动作接口
        /// </summary>
        public Action<string, System.Action> AddToMenu_Action { private get; set; }
        /// <summary>
        /// 获取所有的播放列表
        /// </summary>
        public Func<IList<PlayList>> GetPlayList_Action { private get; set; }
        /// <summary>
        /// 改变播放列表
        /// </summary>
        public Func<PlayList, bool> ChangePlayList_Action { private get; set; }

        #endregion

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void LoadData()
        {
            IList<PlayList> playList = GetPlayList_Action();
            IList<PlayList> downloadList = new List<PlayList>();
            if (playList != null)
            {
                foreach (var playList_item in playList)
                {
                    if (playList_item != null)
                    {
                        PlayList newPlayList = new PlayList()
                        {
                            ID = playList_item.ID,
                            Name = playList_item.Name,
                            Type = playList_item.Type,
                            Muisic = new Dictionary<Guid, MusicItem>()
                        };

                        foreach (var item in playList_item.Muisic)
                        {
                            if (item.Value.Type == Interface.Model.Enum.MusicSourceType.Web)
                            {
                                newPlayList.Muisic.Add(item.Key, item.Value);
                                DownloadData.Add(new Model.DownloadMusicItem(item.Value));
                            }
                        }
                        downloadList.Add(newPlayList);
                    }
                }
            }
        }
    }
}
